package com.extension.company.view.store;


import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bumptech.glide.Glide;
import com.extension.company.admin.R;
import com.extension.company.admin.databinding.ActivityStoreMainBinding;

import java.util.ArrayList;
import java.util.List;

public class StoreMainActivity extends AppCompatActivity {

	private ActivityStoreMainBinding binding;
	private GridLayoutManager manager;
	private List<String> list = new ArrayList<>();
	private String[] titleTable = new String[]{"item1", "item2", "item3", "item4", "item5", "item6", "item7", "item8", "item9", "item10"};
	private List<String> bannerUrl = new ArrayList<>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		binding = DataBindingUtil.setContentView(this, R.layout.activity_store_main);
		binding.storeToolbar.setNavigationIcon(R.drawable.white_back);
		setSupportActionBar(binding.storeToolbar);
//		binding.storeCollapsingtollbarlayout.setTitleEnabled(false);
		binding.storeCollapsingtollbarlayout.setCollapsedTitleGravity(Gravity.CENTER); // 收缩的时候title的位置
		binding.storeCollapsingtollbarlayout.setExpandedTitleGravity(Gravity.CENTER); // 设置展开的布局中title显示居中
		binding.storeCollapsingtollbarlayout.setExpandedTitleColor(getResources().getColor(R.color.black)); //  展开的时候title的颜色
		binding.storeCollapsingtollbarlayout.setCollapsedTitleTextColor(getResources().getColor(R.color.black)); //折叠的时候title的颜色
		binding.storeAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
			@Override
			public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
				int offsetMove = Math.abs(i);
				Log.d("zwk", "移动的距离" + offsetMove);
				//标题栏的渐变
				binding.storeToolbar.setBackgroundColor(changeAlpha(getResources().getColor(R.color.white)
						, Math.abs(i * 1.0f) / appBarLayout.getTotalScrollRange()));
				Log.d("zwk", "获取的高度是" + appBarLayout.getTotalScrollRange());
				if (offsetMove < appBarLayout.getTotalScrollRange() / 2) {
					binding.storeToolbar.setTitle("");
					binding.storeToolbar.setAlpha((appBarLayout.getTotalScrollRange() / 2 - offsetMove * 1.0f) / (appBarLayout.getTotalScrollRange() / 2));
					binding.storeShare.setAlpha((appBarLayout.getTotalScrollRange() / 2 - offsetMove * 1.0f) / (appBarLayout.getTotalScrollRange() / 2));
					binding.storeShare.setImageDrawable(getResources().getDrawable(R.drawable.xiaomi));
					binding.storeToolbar.setNavigationIcon(R.drawable.back2);
					/**
					 * 从最低浮动开始渐显 当前 Offset就是  appBarLayout.getTotalScrollRange() / 2
					 * 所以 Offset - appBarLayout.getTotalScrollRange() / 2
					 */
				} else if (offsetMove > appBarLayout.getTotalScrollRange() / 2) {
					float floate = (offsetMove - appBarLayout.getTotalScrollRange() / 2) * 1.0f / (appBarLayout.getTotalScrollRange() / 2);
					binding.storeToolbar.setAlpha(floate);
					binding.storeShare.setAlpha(floate);
					binding.storeToolbar.setNavigationIcon(R.drawable.back2);
					binding.storeShare.setImageDrawable(getResources().getDrawable(R.drawable.share_link));
					binding.storeToolbar.setTitle("禄福来精品翡翠");
					binding.storeToolbar.setAlpha(floate);
				}
			}

		});

		for (int i = 0; i < 120; i++) {
			list.add("内容:------------" + i);
		}
		bannerUrl.add("https://www.wanandroid.com/blogimgs/7010eea2-16cc-4370-a3ca-9d372170f263.png");
		bannerUrl.add("https://www.wanandroid.com/blogimgs/62c1bd68-b5f3-4a3c-a649-7ca8c7dfabe6.png");
		bannerUrl.add("https://www.wanandroid.com/blogimgs/90c6cc12-742e-4c9f-b318-b912f163b8d0.png");

		manager = new GridLayoutManager(this, 2);
		binding.storeRecycleview.setLayoutManager(manager);
		StoreMainAdapter adapter = new StoreMainAdapter(this, list);
		binding.storeRecycleview.setAdapter(adapter);

		for (int i = 0; i < titleTable.length; i++) {
			binding.storeTablayout.addTab(binding.storeTablayout.newTab().setText(titleTable[i]));
		}


		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
			binding.storeRecycleview.setOnScrollChangeListener(new View.OnScrollChangeListener() {
				@Override
				public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
					switch (manager.findFirstVisibleItemPosition()) {
						case 1:
							binding.storeTablayout.setScrollPosition(1, 0, true);
							break;
						case 10:
							binding.storeTablayout.setScrollPosition(2, 0, true);
							break;
						case 20:
							binding.storeTablayout.setScrollPosition(3, 0, true);
							break;
						case 30:
							binding.storeTablayout.setScrollPosition(4, 0, true);
							break;
						case 40:
							binding.storeTablayout.setScrollPosition(5, 0, true);
							break;
						case 50:
							binding.storeTablayout.setScrollPosition(6, 0, true);
							break;
						case 60:
							binding.storeTablayout.setScrollPosition(7, 0, true);
							break;
						case 70:
							binding.storeTablayout.setScrollPosition(8, 0, true);
							break;
						case 80:
							binding.storeTablayout.setScrollPosition(9, 0, true);
							break;
						case 90:
							binding.storeTablayout.setScrollPosition(10, 0, true);
							break;
						default:
							break;
					}

				}
			});
		}

		binding.storeTablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
			@Override
			public void onTabSelected(TabLayout.Tab tab) {
				switch (tab.getPosition()) {
					case 1:
						manager.scrollToPositionWithOffset(1, 0);
						break;
					case 2:
						manager.scrollToPositionWithOffset(10, 0);
						break;
					case 3:
						manager.scrollToPositionWithOffset(20, 0);
						break;
					case 4:
						manager.scrollToPositionWithOffset(30, 0);
						break;
					case 5:
						manager.scrollToPositionWithOffset(40, 0);
						break;
					case 6:
						manager.scrollToPositionWithOffset(50, 0);
						break;
					case 7:
						manager.scrollToPositionWithOffset(60, 0);
						break;
					case 8:
						manager.scrollToPositionWithOffset(70, 0);
						break;
					case 9:
						manager.scrollToPositionWithOffset(80, 0);
						break;
					case 10:
						manager.scrollToPositionWithOffset(90, 0);
						break;
					default:
						break;
				}
			}

			@Override
			public void onTabUnselected(TabLayout.Tab tab) {

			}

			@Override
			public void onTabReselected(TabLayout.Tab tab) {

			}
		});

		binding.storeBanner.setPages(new CBViewHolderCreator() {
			@Override
			public Holder createHolder(View itemView) {
				return new BannerHolder(itemView);
			}

			@Override
			public int getLayoutId() {
				return R.layout.store_item;
			}
		}, bannerUrl)
				.setPageIndicator(new int[]{R.drawable.indicater_white, R.drawable.indicater_red})
				.setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL);
	}

	protected class BannerHolder extends Holder<String> {
		ImageView bannerImg;

		public BannerHolder(View itemView) {
			super(itemView);
		}

		@Override
		protected void initView(View itemView) {
			bannerImg = itemView.findViewById(R.id.store_item_img);
		}

		@Override
		public void updateUI(String data) {
			Glide.with(StoreMainActivity.this).load(data).into(bannerImg);
		}
	}

	/**
	 * 根据百分比改变颜色透明度
	 */
	public int changeAlpha(int color, float fraction) {
		int alpha = (int) (Color.alpha(color) * fraction);
		return Color.argb(alpha, 255, 255, 255);
	}
}
